package cn.feng.thinkInJava.a9_3_容器.数组.arrays.a16_07_05;

//: arrays/ArraySearching.java
// Using Arrays.binarySearch().
import java.util.*;
import net.mindview.util.*;
import static net.mindview.util.Print.*;

/**
 * 在已排序的数组中查找
 * 
 * @author fengyu
 * @date 2015年8月9日
 */
public class ArraySearching
{
	public static void main(String[] args)
	{
		Generator<Integer> gen=new RandomGenerator.Integer(1000);
		int[] a=ConvertTo.primitive(Generated.array(new Integer[25],gen));
		Arrays.sort(a);
		print("Sorted array: "+Arrays.toString(a));
		while(true)
		{
			int r=gen.next();
			int location=Arrays.binarySearch(a,r);
			if(location>=0)
			{
				print("Location of "+r+" is "+location+", a["+location+"] = "+a[location]);
				break; // Out of while loop
			}
		}
	}
} /* Output:
Sorted array: [128, 140, 200, 207, 258, 258, 278, 288, 322, 429, 511, 520, 522, 551, 555, 589, 693, 704, 809, 861, 861, 868, 916, 961, 998]
Location of 322 is 8, a[8] = 322
*///:~
